//
//  LBRMViewController.m
//  LBReact
//
//  Created by WuShiHai on 19/5/24.
//  Copyright (c) 2019 Lebbay. All rights reserved.
//

#import "LBRPViewController.h"

#import "LBReactMultiPatcher.h"

@interface LBRPViewController ()

@end

@implementation LBRPViewController

- (instancetype)initWithURL:(NSString *)URLString
                  component:(NSString *)component
                   userInfo:(NSDictionary *)userInfo {
    
    if (self = [super initWithURL:URLString component:component userInfo:userInfo]) {
    }
    
    return self;
}

- (void)dealloc {
    if (self.bridge) {
        [LBReactBridgeManager decrease:self.bridge];
    }
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)renderInMultiMode:(NSString *)component {
    /**
     1.启动去更新本地所拥有的补丁包。 done
     2.将内置、本地已下载进行择新选择，并加载筛选后的全部 done
     3.渲染页面 done
     
     备注：1.根据version进行版本比较，判断是否更新，暂时可不做。 done
          2.每隔多久进行一次更新，暂时可不做。
          3.需要比较最新的模块是否有改动，判断是否更新，获取文件md5，在保存接口进行判断，重要！！！ done
     
     待做：
     1.前后台切换下载最新补丁，但不使用。 done
     2.加载的时候，判断本地是否拥有更新的补丁，如果有，则优先使用最新补丁。 done
     3.内置业务包，重要！！！ done
     *
     **/
    NSString *module = [self moduleWith:component];
    LBBridge *bridge = [LBReactBridgeManager bridgeWithComponent:component];
    if (bridge) {
        //有什么用什么，暂不支持补丁版本比较
//        NSInteger index = [bridge indexForModule:module];
//        LBReactPatcherModel *patcher = [LBReactMultiPatcherManager localLatestPatcherWith:module];
//        if (patcher.bundleId > index) {
//            [self renderInNewMainBridge:module
//                                patcher:patcher];
//        } else {
//            self.bridge = bridge;
//            [LBReactBridgeManager increase:bridge];
//            [self loadComponent];
//        }
        
        self.bridge = bridge;
        [LBReactBridgeManager increase:bridge];
        [self loadComponent];
        
    } else {
        [self renderInNewMainBridge:module
                            patcher:nil];
    }
}

- (void)renderInNewMainBridge:(NSString *)module
                      patcher:(LBReactPatcherModel *)patcher {
    void (^complete)(RCTBridge *bridge, NSError *error) = ^(RCTBridge *bridge, NSError *error) {
        if (bridge) {
            self.bridge = bridge;
            [LBReactBridgeManager increase:bridge];
            void(^complete)(BOOL isSuccess, NSError *error, BOOL isUpdate) = ^(BOOL isSuccess, NSError *error, BOOL isUpdate) {
                if (isSuccess && [self isRegisterComponent:self.componentName]) {
                    [self loadComponent];
                }
            };
            if (patcher) {
                [LBReactMultiPatcherManager renderLocalBusinessBundleWith:patcher
                                                                   bridge:bridge
                                                                 complete:complete];
            } else {
                [LBReactMultiPatcherManager renderBusinessBundle:LBReactBridgeRenderDefaultLevel
                                                          bridge:bridge
                                                          module:module
                                                        complete:complete];
            }
        }
    };
    [LBReactBridgeManager fetchMainBrdige:complete];
}

- (NSString *)moduleWith:(NSString *)component {
    return [[component componentsSeparatedByString:@"/"] firstObject];
}

@end
